package com.example.demo.javatest.thread;

/**
 *
 * volatile 关键字测试用例
 * Created by liujiahao on 2019/1/31.
 */
public class MyThread extends Thread {
    private volatile boolean isRunning = true;
    private void setRunning(boolean isRunning) {
        this.isRunning = isRunning;
    }
    @Override
    public void run() {
        System.out.println("...进入run方法...");
        while(isRunning==true) {
            //boolean a = isRunning;
        }
        System.out.println("线程停止");
    }
    public static void main(String[] args) throws InterruptedException {
        MyThread rt = new MyThread();
        Thread t1=new Thread(new Runnable(){
            @Override
            public void run() {
                System.out.println("hahhaa");
                /*try {
                    rt.fun2();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
            }
        },"t1");
        Thread t2=new Thread(new Runnable(){
            @Override
            public void run() {
                System.out.println("hahahah1");
                /*try {
                    rt.fun2();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
            }
        },"t2");
        Thread t3=new Thread(new Runnable(){
            @Override
            public void run() {
                System.out.println("hahahah2");
                /*try {
                    rt.fun2();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }*/
            }
        },"t3");
        t1.start();
        t2.start();
        t3.start();
    }
    public static void fun1() throws InterruptedException{
        MyThread rt = new MyThread();
        rt.start();
        Thread.sleep(3000);
        rt.setRunning(false);
        System.out.println("isRunning的值已经被设置了false");
        Thread.sleep(1000);
        System.out.println(rt.isRunning);
    }
    public    void fun2() throws InterruptedException{
        System.out.println("one"+Thread.currentThread().getName());
        /*this.notify();
        System.out.println("two"+Thread.currentThread().getName());*/
    }

}
